Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
Hey Guys,
I am displaying a list of "News Articles" on a page but the client wants the list to refelect the newest "News Article" you have created - not using umbraco's node stack order.
Here is my XSLT code so far - this includes a paginator.
<!-- Pagination Variables --> <xsl:variable name="recordsPerPage" select="5"/><xsl:variable name="pageNumber" > <xsl:choose> <xsl:when test="umbraco.library:RequestQueryString('page') <= 0 or string(umbraco.library:RequestQueryString('page')) = '' or string(umbraco.library:RequestQueryString('page')) = 'NaN'"> 0 </xsl:when> <xsl:otherwise> <xsl:value-of select="umbraco.library:RequestQueryString('page')" /> </xsl:otherwise> </xsl:choose></xsl:variable><xsl:variable name="numberOfRecords" select="count($currentPage/NewsArticles/NewsArticle)" /> <!-- List Articles --> <ul id="list-news-articles"> <xsl:for-each select="$currentPage/NewsArticles/NewsArticle"> <xsl:if test="position() > $recordsPerPage * number($pageNumber) and position() <= number($recordsPerPage * number($pageNumber) + $recordsPerPage )"> <li class="clearfix"> <a href="{umbraco.library:NiceUrl(@id)}"> <xsl:if test="articleThumb > 0"> <xsl:variable name="img" select="umbraco.library:GetMedia(articleThumb, 0)" /> <xsl:variable name="imgPath" select="concat(substring-before($img/umbracoFile,'.'), '.jpg')" /> <img class="person-image" src="/ImageGen.ashx?image={$imgPath}&width=150&" alt="{@nodeName}" /> </xsl:if> </a> <a class="list-news-heading" href="{umbraco.library:NiceUrl(@id)}"> <xsl:value-of select="@nodeName" /> </a> <div class="article-brief"> <span class="featured-date"> <xsl:value-of select="newsArticleDate" />, <xsl:value-of select="newsArticleCategory" /> </span> <p> <xsl:value-of select="articleBrief" /> <a class="read-more" href="{umbraco.library:NiceUrl(@id)}"> Read More... </a> </p> </div> </li> </xsl:if> </xsl:for-each></ul> <!-- Paginator --> <ul class="pagination clearfix"> <li class="prev"> <xsl:if test="$pageNumber > 0"> <a href="?page={$pageNumber -1}">Prev</a> </xsl:if> </li> <li class="pages"> <xsl:call-template name="for.loop"> <xsl:with-param name="i">1</xsl:with-param> <xsl:with-param name="page" select="$pageNumber +1"></xsl:with-param> <xsl:with-param name="count" select="ceiling(count($currentPage/NewsArticles/NewsArticle) div $recordsPerPage)" /> </xsl:call-template> </li> <li class="next"> <xsl:if test="(($pageNumber +1 ) * $recordsPerPage) < ($numberOfRecords)"> <a href="?page={$pageNumber +1}">Next</a> </xsl:if> </li></ul> </xsl:template> <!-- XSLT Template For Loop DO NOT EDIT--> <xsl:template name="for.loop"> <xsl:param name="i"/> <xsl:param name="count"/> <xsl:param name="page"/> <xsl:if test="$i <= $count"> <xsl:if test="$page != $i"> <a href="{umbraco.library:NiceUrl($currentPage/@id)}?page={$i - 1}" > <xsl:value-of select="$i" /> </a> </xsl:if> <xsl:if test="$page = $i"> <span class="current-page"> <xsl:value-of select="$i" /> </span> </xsl:if> </xsl:if> <xsl:if test="$i <= $count"> <xsl:call-template name="for.loop"> <xsl:with-param name="i"> <xsl:value-of select="$i + 1"/> </xsl:with-param> <xsl:with-param name="count"> <xsl:value-of select="$count"/> </xsl:with-param> <xsl:with-param name="page"> <xsl:value-of select="$page"/> </xsl:with-param> </xsl:call-template> </xsl:if></xsl:template>
All suggestions are welcome - if you have any questions I will be watching the thread.
Jordy
Sorry forgot a vital piece of info - The client wants the "latest news article" to display first on the front end.
You may have already got that but just being clear.
Hi Jordy,
You want to sort the articles by date right? So you'll need to add an XSLT sort element to your loop:
<xsl:for-each select="$currentPage/NewsArticles/NewsArticle"> <xsl:sort select="articleDate" order="ascending" />
...where 'articleDate' is the alias of your article date property.
Yes by "created date".
I have an alias within the doc type named "newsArticleDate" however, this doesn't use a date picker - the user just inputs text into the field.
Will I need to use the umbraco date picker in order for this to "order" correctly?
Is it possible to pick up the "Created" property on the node within the "properties" tab? If so I think that may be a better solution.
Thanks for your post Dan.
Sure, the createdDate is stored in the XML cache so you can use this to sort by, no probs:
<xsl:sort select="@createDate" order="ascending" />
As a point of interest, to give you an idea of the data that's actually in your selection you can do something like this:
<textarea><xslt:copy-of select="$currentPage/NewsArticles/NewsArticle" /></textarea>
I just find this sometimes helps to actually see the data you're manipulating through XSLT.
That works a treat - thanks Dan!
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
Umbraco Node Sorting | XSLT
Hey Guys,
I am displaying a list of "News Articles" on a page but the client wants the list to refelect the newest "News Article" you have created - not using umbraco's node stack order.
Here is my XSLT code so far - this includes a paginator.
<!-- Pagination Variables -->
<xsl:variable name="recordsPerPage" select="5"/>
<xsl:variable name="pageNumber" >
<xsl:choose>
<xsl:when test="umbraco.library:RequestQueryString('page') <= 0 or string(umbraco.library:RequestQueryString('page')) = '' or string(umbraco.library:RequestQueryString('page')) = 'NaN'">
0
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="umbraco.library:RequestQueryString('page')" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="numberOfRecords" select="count($currentPage/NewsArticles/NewsArticle)" />
<!-- List Articles -->
<ul id="list-news-articles">
<xsl:for-each select="$currentPage/NewsArticles/NewsArticle">
<xsl:if test="position() > $recordsPerPage * number($pageNumber) and position() <= number($recordsPerPage * number($pageNumber) + $recordsPerPage )">
<li class="clearfix">
<a href="{umbraco.library:NiceUrl(@id)}">
<xsl:if test="articleThumb > 0">
<xsl:variable name="img" select="umbraco.library:GetMedia(articleThumb, 0)" />
<xsl:variable name="imgPath" select="concat(substring-before($img/umbracoFile,'.'), '.jpg')" />
<img class="person-image" src="/ImageGen.ashx?image={$imgPath}&width=150&" alt="{@nodeName}" />
</xsl:if>
</a>
<a class="list-news-heading" href="{umbraco.library:NiceUrl(@id)}">
<xsl:value-of select="@nodeName" />
</a>
<div class="article-brief">
<span class="featured-date">
<xsl:value-of select="newsArticleDate" />,
<xsl:value-of select="newsArticleCategory" />
</span>
<p>
<xsl:value-of select="articleBrief" />
<a class="read-more" href="{umbraco.library:NiceUrl(@id)}">
Read More...
</a>
</p>
</div>
</li>
</xsl:if>
</xsl:for-each>
</ul>
<!-- Paginator -->
<ul class="pagination clearfix">
<li class="prev">
<xsl:if test="$pageNumber > 0">
<a href="?page={$pageNumber -1}">Prev</a>
</xsl:if>
</li>
<li class="pages">
<xsl:call-template name="for.loop">
<xsl:with-param name="i">1</xsl:with-param>
<xsl:with-param name="page" select="$pageNumber +1"></xsl:with-param>
<xsl:with-param name="count" select="ceiling(count($currentPage/NewsArticles/NewsArticle) div $recordsPerPage)" />
</xsl:call-template>
</li>
<li class="next">
<xsl:if test="(($pageNumber +1 ) * $recordsPerPage) < ($numberOfRecords)">
<a href="?page={$pageNumber +1}">Next</a>
</xsl:if>
</li>
</ul>
</xsl:template>
<!-- XSLT Template For Loop DO NOT EDIT-->
<xsl:template name="for.loop">
<xsl:param name="i"/>
<xsl:param name="count"/>
<xsl:param name="page"/>
<xsl:if test="$i <= $count">
<xsl:if test="$page != $i">
<a href="{umbraco.library:NiceUrl($currentPage/@id)}?page={$i - 1}" >
<xsl:value-of select="$i" />
</a>
</xsl:if>
<xsl:if test="$page = $i">
<span class="current-page">
<xsl:value-of select="$i" />
</span>
</xsl:if>
</xsl:if>
<xsl:if test="$i <= $count">
<xsl:call-template name="for.loop">
<xsl:with-param name="i">
<xsl:value-of select="$i + 1"/>
</xsl:with-param>
<xsl:with-param name="count">
<xsl:value-of select="$count"/>
</xsl:with-param>
<xsl:with-param name="page">
<xsl:value-of select="$page"/>
</xsl:with-param>
</xsl:call-template>
</xsl:if>
</xsl:template>
All suggestions are welcome - if you have any questions I will be watching the thread.
Jordy
Sorry forgot a vital piece of info - The client wants the "latest news article" to display first on the front end.
You may have already got that but just being clear.
Jordy
Hi Jordy,
You want to sort the articles by date right? So you'll need to add an XSLT sort element to your loop:
...where 'articleDate' is the alias of your article date property.
Yes by "created date".
I have an alias within the doc type named "newsArticleDate" however, this doesn't use a date picker - the user just inputs text into the field.
Will I need to use the umbraco date picker in order for this to "order" correctly?
Is it possible to pick up the "Created" property on the node within the "properties" tab? If so I think that may be a better solution.
Thanks for your post Dan.
Jordy
Sure, the createdDate is stored in the XML cache so you can use this to sort by, no probs:
As a point of interest, to give you an idea of the data that's actually in your selection you can do something like this:
I just find this sometimes helps to actually see the data you're manipulating through XSLT.
That works a treat - thanks Dan!
Jordy
is working on a reply...